package cn.onein.edu.util;

import java.util.Iterator;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import cn.onein.edu.common.PaymentType;

public class PaymentResponseHandler {
	private static final String CHARSET;
	
	private static final String TENPAY_KEY;
	
	private static final String ALIPAY_KEY;
	
	static{
		ResourceBundle paymentInfo = ResourceUtil.getPaymentProperties();
		
		CHARSET = paymentInfo.getString("CHARSET");
		TENPAY_KEY = paymentInfo.getString("TENPAY_KEY");
		
		ALIPAY_KEY = paymentInfo.getString("ALIPAY_KEY");
	}	
	
	/** 应答的参数 */
	private SortedMap<String,String> params = new TreeMap<String,String>();
	
	private PaymentType paymentType = null;
	
	/** debug信息 */
	private String debugInfo = "";
	
	private PaymentResponseHandler(){}
	
	/**
	 * 设置应答参数
	 * @param key
	 * @param value
	 */
	private void setParam(String key, String value){
		if(key != null && value != null){
			String v = "";
			v = value.trim();
			this.params.put(key, v);			
		}
	}
	
	public static PaymentResponseHandler getInstance(PaymentType type, HttpServletRequest request){
		if(type == null){
			throw new IllegalArgumentException("PaymentType must be not null!");
		}
		
		PaymentResponseHandler handler = new PaymentResponseHandler();

		handler.paymentType = type;
		
		Map m = request.getParameterMap();
		Iterator it = m.keySet().iterator();
		while (it.hasNext()) {
			String k = (String) it.next();
			String v = ((String[]) m.get(k))[0];
			handler.setParam(k, v);
		}
		
		return handler;
	}
	
	/**
	 * 是否签名通过,规则是:按参数名称a-z排序,遇到空值的参数不参加签名。
	 * @return boolean
	 */
	public boolean isPaySign() {
		String sign = "";
		String paySign = "";
		StringBuffer sb = new StringBuffer();
		Set es = this.params.entrySet();
		Iterator it = es.iterator();
		
		switch(paymentType){
		case Tenpay:
			while(it.hasNext()) {
				Map.Entry entry = (Map.Entry)it.next();
				String k = (String)entry.getKey();
				String v = (String)entry.getValue();
				if(!"sign".equals(k) && null != v && !"".equals(v)) {
					sb.append(k + "=" + v + "&");
				}
			}
			sb.append("key=" + TENPAY_KEY);
			//算出摘要
			sign = PaymentUtil.MD5Encode(sb.toString(), CHARSET).toUpperCase();
			paySign = this.getParam("sign").toUpperCase();
			break;
		case Alipay:
			while(it.hasNext()) {
				Map.Entry entry = (Map.Entry)it.next();
				String k = (String)entry.getKey();
				String v = (String)entry.getValue();
				if(!"sign".equals(k) && !"sign_type".equals(k) && null != v && !"".equals(v)) {
					sb.append(k + "=" + v + "&");
				}
			}
			sign = sb.substring(0, sb.lastIndexOf("&"));
			sign = sign + ALIPAY_KEY;
			sign = PaymentUtil.MD5Encode(sign, CHARSET);
			paySign = this.getParam("sign");
			break;
		default:
			break;			
		}
		
		//debug信息
		this.debugInfo = sb.toString() + " => sign:" + sign + ";" + paymentType.toString() + "-sign:" + paySign;
		System.out.println(paySign);
		
		return paySign.equals(sign);
	}
	
	public String getParam(String key){
		String value = params.get(key);
		return value == null ? "" : value;
	}
	
	public PaymentType getPaymentType(){
		return paymentType;
	}
	
	public String getDebugInfo(){
		return debugInfo;
	}
}
